package com.iran.protoType.shallowCopy;

import java.util.Date;

/**
 * java 的clone 是浅复制，基本类型还好，如果是复杂类型，复杂类型变化，会影响原型类变化
 */
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        Cat cat = new Cat();
        Date date = new Date();
        date.setTime(23232123L);
        CatBrand catBrand = new CatBrand();
        catBrand.setA("铜");
        cat.setAge(1);
        cat.setName("xiaohua");
        cat.setType("homeCat");
        cat.setCatBrand(catBrand);
        cat.setBorn(date);
        Cat cloneCat = cat.clone();

        System.out.println(cat.getAge());
        System.out.println(cloneCat.getAge());
        System.out.println(cat.getCatBrand().getA());
        System.out.println(cloneCat.getCatBrand().getA());
        System.out.println(cat.getBorn());
        System.out.println(cloneCat.getBorn());

        System.out.println("change");

        //基本类型变化
        cat.setAge(2);
        cat.setType("EnglishCat");
        //复杂类型变化
        catBrand.setA("金子");
        cloneCat.getBorn().setTime(33333333L);


        System.out.println(cloneCat.getAge());
        System.out.println(cat.getAge());

        System.out.println(cloneCat.getType());
        System.out.println(cat.getType());

        System.out.println(cloneCat.getCatBrand().getA());
        System.out.println(cat.getCatBrand().getA());

        System.out.println(cat.getBorn());
        System.out.println(cloneCat.getBorn());
        //浅复制，复杂类型变化，原型对象和克隆对象都变化
        //深复制，复杂类型变化，原型对象无变化，克隆对象变化
        //说明super.clone()方法，复杂类型是引用
    }
}
